<template>
  <nav class="aui-navbar" :class="`aui-navbar--${$store.state.navbarLayoutType}`">
    <div class="aui-navbar__header">
      <h1 class="aui-navbar__brand" @click="$router.push({ name: 'home' })">
        <a class="aui-navbar__brand-lg" href="javascript:;">{{ $t('brand.lg') }}</a>
        <a class="aui-navbar__brand-mini" href="javascript:;">{{ $t('brand.mini') }}</a>
      </h1>
    </div>
    <div class="aui-navbar__body">
      <el-menu class="aui-navbar__menu mr-auto" mode="horizontal">
        <el-menu-item index="1" @click="$store.state.sidebarFold = !$store.state.sidebarFold">
          <svg class="icon-svg aui-navbar__icon-menu aui-navbar__icon-menu--switch" aria-hidden="true"><use xlink:href="#icon-outdent"></use></svg>
        </el-menu-item>
        <el-menu-item index="2" @click="refresh()">
          <svg class="icon-svg aui-navbar__icon-menu aui-navbar__icon-menu--refresh" aria-hidden="true"><use xlink:href="#icon-sync"></use></svg>
        </el-menu-item>
        <!-- <el-menu-item index="3" @click="refresh()">
          <i v-if="!backendTimeStr">未设置目标时间</i>
          <i v-else-if="isTimeout">提交作业已超时 {{ Math.abs(days) }} 天 {{ Math.abs(hours) }} 小时 {{ Math.abs(minutes) }} 分钟 {{ Math.abs(seconds) }} 秒</i>
          <i v-else>距离提交作业时间还有：{{ days }}天 {{ hours }}小时 {{ minutes }}分钟 {{ seconds }} 秒</i>
        </el-menu-item> -->
      </el-menu>
      <el-menu class="aui-navbar__menu" mode="horizontal">
        <el-menu-item index="4" @click="fullscreenHandle()">
          <svg class="icon-svg aui-navbar__icon-menu" aria-hidden="true"><use xlink:href="#icon-fullscreen"></use></svg>
        </el-menu-item>
        <el-menu-item index="5" class="aui-navbar__avatar">
          <el-dropdown placement="bottom" :show-timeout="0">
            <span class="el-dropdown-link">
              <img :src="$store.state.user.headUrl">
              <span>{{ $store.state.user.name }}</span>
              <i class="el-icon-arrow-down"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()">{{ $t('updatePassword.title') }}</el-dropdown-item>
              <el-dropdown-item @click.native="logoutHandle()">{{ $t('logout') }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password v-if="updatePasswordVisible" ref="updatePassword"></update-password>
  </nav>
</template>
<script>
import screenfull from 'screenfull'
import UpdatePassword from './main-navbar-update-password'
import { clearLoginInfo } from '@/utils'
export default {
  inject: ['refresh'],
  data () {
    return {
      updatePasswordVisible: false,
      messageTip: false,
      days: 0,
      hours: 0,
      minutes: 0,
      seconds: 0,
      isTimeout: false,
      backendTimeStr: null
    }
  },
  // mounted () {
  //   console.log('id' + this.$store.state.user.deadLine)
  //   // 后端传递的时间字符串
  //   this.backendTimeStr = this.$store.state.user.deadLine;
  //   if (this.backendTimeStr !== null) {
  //     var backendTime = Date.parse(this.backendTimeStr + ' GMT+0800'); // 解析时间字符串并转换为时间戳
  //     this.calculateCountdown(backendTime);
  //     setInterval(() => {
  //       this.calculateCountdown(backendTime);
  //     }, 1000); // 更新间隔为每秒
  //   }
  // },
  components: {
    UpdatePassword
  },
  methods: {
    // 全屏
    fullscreenHandle () {
      if (!screenfull.enabled) {
        return this.$message({
          message: this.$t('fullscreen.prompt'),
          type: 'warning',
          duration: 500
        })
      }
      screenfull.toggle()
    },
    // 修改密码
    updatePasswordHandle () {
      this.updatePasswordVisible = true
      this.$nextTick(() => {
        this.$refs.updatePassword.init()
      })
    },
    // 退出
    logoutHandle () {
      this.$confirm(this.$t('prompt.info', { 'handle': this.$t('logout') }), this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$http.post('/logout').then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          clearLoginInfo()
          this.$router.push({ name: 'login' })
        }).catch(() => {})
      }).catch(() => {})
    },
    // 倒计时
    calculateCountdown(backendTime) {
      var currentTime = new Date().getTime(); // 获取当前时间的时间戳
      var timeDiff = currentTime - backendTime; // 倒计时逻辑修正在此
      if (timeDiff < 0) {
        this.isTimeout = false; // 修改逻辑，目标时间未到，不超时
        this.days = Math.floor(-timeDiff / (1000 * 60 * 60 * 24)); // 计算剩余天数
        this.hours = Math.floor((-timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); // 计算剩余小时数
        this.minutes = Math.floor((-timeDiff % (1000 * 60 * 60)) / (1000 * 60)); // 计算剩余分钟数
        this.seconds = Math.floor((-timeDiff % (1000 * 60)) / 1000); // 计算剩余秒数
      } else {
        this.isTimeout = true; // 目标时间已过，超时
        this.days = Math.floor(timeDiff / (1000 * 60 * 60 * 24)); // 计算超时天数
        this.hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); // 计算超时小时数
        this.minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60)); // 计算超时分钟数
        this.seconds = Math.floor((timeDiff % (1000 * 60)) / 1000); // 计算超时秒数
      }
    }
  }
}
</script>
